/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interfaz;

import Negocio.Login;
import Negocio.TipoUsuario;
import Negocio.UsuarioLogin;
import Negocio.Usuarios;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author Jorge
 */
public class LoginInterfaz extends javax.swing.JFrame {
    UsuarioInterfaz ui;
    CajeroInterfaz ci;
    AdministradorInterfaz ai;
    GerenteInterfaz gi;
    Usuarios usuario = new Usuarios();
    Login  login = new Login();
    UsuarioLogin usuarioLogin = new UsuarioLogin();
    TipoUsuario tipoUsuario = new TipoUsuario();
    Validacion validar = new Validacion();
    Date hoy = new Date();    
    Calendar calendario = Calendar.getInstance();
    SimpleDateFormat formato;
    int hora, minutos, segundos;
    /**
     * Creates new form Usuarios
     */
    public LoginInterfaz() {
        initComponents();
        formato=new SimpleDateFormat("yyyy-MM-dd");
        lblFechaLog.setText(formato.format(hoy));
        hora =calendario.get(Calendar.HOUR_OF_DAY);
        minutos = calendario.get(Calendar.MINUTE);
        segundos = calendario.get(Calendar.SECOND);
        lblHoraLog.setText(hora+":"+minutos);
        ui=new UsuarioInterfaz();
        ci=new CajeroInterfaz();
        ai=new AdministradorInterfaz();
        gi=new GerenteInterfaz();
        ui.setVisible(false);
        ci.setVisible(false);
        ai.setVisible(false);
        gi.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtUsuario = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        btnlogin = new javax.swing.JButton();
        btnNuevoUser = new javax.swing.JButton();
        txtContraseña = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        lblFechaLog = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lblHoraLog = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Usuario:");

        jLabel2.setText("Contraseña:");

        btnlogin.setText("Log In");
        btnlogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnloginActionPerformed(evt);
            }
        });

        btnNuevoUser.setText("Nuevo Usuario");
        btnNuevoUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoUserActionPerformed(evt);
            }
        });

        jLabel3.setText("Fecha:");

        lblFechaLog.setText("jLabel4");

        jLabel5.setText("Hora:");

        lblHoraLog.setText("jLabel5");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(75, 75, 75)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtUsuario)
                            .addComponent(txtContraseña, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(9, 9, 9)
                                .addComponent(btnNuevoUser))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(lblFechaLog, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)
                            .addComponent(lblHoraLog, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(167, 167, 167)
                        .addComponent(btnlogin)))
                .addContainerGap(108, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(lblFechaLog))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(lblHoraLog))
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtContraseña, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addComponent(btnlogin)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnNuevoUser)
                .addContainerGap(85, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnloginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnloginActionPerformed
        // TODO add your handling code here:
        usuario.setCedula(txtUsuario.getText());
        usuario.buscar();        
        if(txtUsuario.getText().compareTo(usuario.getCedula())==0 && txtContraseña.getText().compareTo(usuario.getContraseña())==0){
            tipoUsuario.setUsuario(usuario);
            tipoUsuario.buscar(); 
            if(tipoUsuario.getTipo().getId()==1){
                ui.setVisible(false);
                ci.setVisible(false);
                ai.setVisible(false);
                gi.setVisible(true);
                this.setVisible(false);                
            } 
            else if(tipoUsuario.getTipo().getId()==2){
                ui.setVisible(false);
                ci.setVisible(false);
                ai.setVisible(true);
                gi.setVisible(false);
                this.setVisible(false);
            }
            else if(tipoUsuario.getTipo().getId()==3){
                ui.setVisible(false);
                ci.setVisible(true);
                ai.setVisible(false);
                gi.setVisible(false);
                this.setVisible(false);
            }
            login.setFecha(lblFechaLog.getText());
            login.setHora(lblHoraLog.getText());
            login.grabar();
            usuarioLogin.setUsuario(usuario);
            usuarioLogin.grabar();
        }
    }//GEN-LAST:event_btnloginActionPerformed

    private void btnNuevoUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoUserActionPerformed
        // TODO add your handling code here:
        ui.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_btnNuevoUserActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        
        
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LoginInterfaz().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnNuevoUser;
    private javax.swing.JButton btnlogin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel lblFechaLog;
    private javax.swing.JLabel lblHoraLog;
    private javax.swing.JPasswordField txtContraseña;
    private javax.swing.JTextField txtUsuario;
    // End of variables declaration//GEN-END:variables
}
